package com.jiangyg.mall.db.hibernate.mapper;

import com.jiangyg.mall.core.exception.SqlMapperException;
import com.jiangyg.mall.core.support.cache.CacheAdapter;
import com.jiangyg.mall.core.utils.VelocityUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;
import java.util.Map;

/**
 * 类描述：命令查询集合
 *
 * @author jiangyg
 * @version 2.0
 * @date 2019-10-10
 */
public class SQLMapper implements InitializingBean {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(SQLMapper.class);

    /**
     * 缓存适配器
     */
    private CacheAdapter cacheAdapter;

    /**
     * sql-query映射文件路径集合
     */
    private List<String> mappingLocations;

    /**
     * 扩展参数
     */
    private Map<String, Object> extParams;

    /**
     * 功能描述：获取执行SQL字符串
     *
     * @param name 命令
     * @return 执行SQL字符串
     */
    public String getSqlByNamed(final String name) {
        return this.getSqlByNamed(name, null);
    }

    /**
     * 功能描述：获取执行SQL字符串
     *
     * @param name   命名
     * @param params 参数
     * @return 执行SQL字符串
     */
    public <T> String getSqlByNamed(final String name, final T params) {
        String queryString = null;
        try {
            if (StringUtils.isNotBlank(name)) {
                SQLObject object = cacheAdapter.get(name);
                if (object == null) {
                    return null;
                }
                queryString = VelocityUtils.stringEvaluate(object.getSqlStr(), params, extParams);
            }
            if (StringUtils.isBlank(queryString)) {
                throw new SqlMapperException("获取执行SQL异常：命名[" + name + "]的SQL为空！");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("获取执行SQL[" + name + "]：" + queryString);
            }
        } catch (Exception ex) {
            logger.error("获取执行SQL[" + name + "]出错：", ex);
            throw new SqlMapperException("获取执行SQL[" + name + "]出错", ex);
        }
        return queryString;
    }

    /**
     * 功能描述：获取统计执行SQL
     *
     * @param name 命名
     * @return 统计执行SQL
     */
    public String getCountSqlByNamed(final String name) {
        return this.getCountSqlByNamed(name, null);
    }

    /**
     * 功能描述：获取统计执行SQL
     *
     * @param name   命名
     * @param params 参数
     * @return 统计执行SQL
     */
    public <T> String getCountSqlByNamed(final String name, final T params) {
        return "select count(1) from (" + this.getSqlByNamed(name, params) + ") t";
    }

    public void setCacheAdapter(CacheAdapter cacheAdapter) {
        this.cacheAdapter = cacheAdapter;
    }

    public void setMappingLocations(List<String> mappingLocations) {
        this.mappingLocations = mappingLocations;
    }

    public void setExtParams(Map<String, Object> extParams) {
        this.extParams = extParams;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 解析SQL文件
        SQLXmlResolver resolver = new SQLXmlResolver(cacheAdapter, mappingLocations);
        resolver.execute();
    }

}
